package com.spark.lola.service.gds.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.gds.ProductInfo;
import com.spark.lola.mapper.gds.ProductInfoMapper;
import com.spark.lola.mapper.pur.OrderProductMapper;
import com.spark.lola.service.gds.IProductInfoService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.mybatis.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dengyejun
 * @since 2020-04-27
 */
@Service
public class ProductInfoServiceImpl extends BaseServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Override
    public List<SelectMap> selectMap(Long categoryId, String key) {
        QueryWrapper<ProductInfo> queryWrapper =new QueryWrapper<>();
        queryWrapper.select("id,no,name");
        queryWrapper.eq("used", CommonConstant.Y);
        if(categoryId.intValue() > 0) {
            queryWrapper.eq("category_id", categoryId);
        }
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(o->o.like("no",key).or().like("name",key));
        }
        List<ProductInfo> lst = baseMapper.selectList(queryWrapper);
        List<SelectMap> selectMaps =new ArrayList<>();
        if(!CollectionUtils.isEmpty(lst)) {
            lst.forEach(item -> {
                selectMaps.add(new SelectMap(item.getId().toString(), item.getName(),item.getNo()));
            });
        }
        return selectMaps;
    }

    @Override
    public List<ProductInfo> getListByCategoryId(Long categoryId, String key) {
        QueryWrapper<ProductInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id,no,name");
        queryWrapper.eq("used", CommonConstant.Y);
        if (categoryId.intValue() > 0) {
            queryWrapper.eq("category_id", categoryId);
        }
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(o -> o.like("no", key).or().like("name", key));
        }
        List<ProductInfo> lst = baseMapper.selectList(queryWrapper);
        return lst;
    }

    @Override
    public Map<String,BigDecimal> supStatistics(Long productId, String start, String end) {
        Map<String,BigDecimal> map =new HashMap<>();
        List<Map<String,Object>> sales = orderProductMapper.sales(productId,start,end);
        if(!CollectionUtils.isEmpty(sales) && sales.get(0)!=null){
            sales.get(0).keySet().forEach(key->{
                map.put(key,new BigDecimal(sales.get(0).get(key).toString()));
            });
        }
        return map;
    }

}
